﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Customer_ProductDetails : System.Web.UI.Page
{
   static DataTable tbGioHang = new DataTable();
    CustormerMethord cm = new CustormerMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        
        if (!IsPostBack)
        {
            ProductDetails();
            
            if (Session["Shopping"] != null)
            {
                tbGioHang = Session["Shopping"] as DataTable;
            }
            else {
                tbGioHang.Rows.Clear();
                tbGioHang.Columns.Clear();
                tbGioHang.Columns.Add("ProductID",typeof(int));
                tbGioHang.Columns.Add("ProductName",typeof(string));
                tbGioHang.Columns.Add("Catelogy", typeof(string));
                tbGioHang.Columns.Add("Quantity", typeof(int));
                tbGioHang.Columns.Add("Weight",typeof(float));
                tbGioHang.Columns.Add("Width", typeof(float));
                tbGioHang.Columns.Add("Lenght", typeof(float));
                tbGioHang.Columns.Add("Price", typeof(float));
                tbGioHang.Columns.Add("Total", typeof(float),"Quantity * Price");
            }
            
        }
    }
    public void ProductDetails() {
        DataTable dt = new DataTable();
        if (Request.QueryString["ProID"]== null) {
            Response.Redirect("Home.aspx");
        }
        int ProductId = Convert.ToInt32(Request.QueryString["ProID"].ToString());
        dt = cm.ProductDetails(ProductId);
        lblProductId.Text = dt.Rows[0][0].ToString();
        lblCatelgory.Text = dt.Rows[0][1].ToString();
        lblProductName.Text = dt.Rows[0][2].ToString();
        lblQuantity.Text = dt.Rows[0][3].ToString();
        ibnImager.ImageUrl = dt.Rows[0][4].ToString();
        lblWeight.Text = dt.Rows[0][5].ToString();
        lblWidth.Text = dt.Rows[0][6].ToString();
        lblLenght.Text = dt.Rows[0][7].ToString();
        lblPrice.Text = dt.Rows[0][8].ToString();
        txtDescription.Text = dt.Rows[0][9].ToString();
       
    } 

    
    protected void btnShoppingCart_Click(object sender, EventArgs e)
    {
        int intProductID = Convert.ToInt32(Request.QueryString["ProID"].ToString());;
                string strProductName = lblProductName.Text;
                string strCatelogy = lblCatelgory.Text;
                int intQuantity = 1;
                float Weight =float.Parse(lblWeight.Text);

                float Width =float.Parse(lblWidth.Text);
                float Lenght =float.Parse(lblLenght.Text);
                float Price = float.Parse(lblPrice.Text);

                foreach (DataRow row in tbGioHang.Rows)
                {
                    if ((int)row["ProductId"] == intProductID)
                    {
                        row["Quantity"] = (int)row["Quantity"] + 1;
                        goto GioHang;
                    }
                }
                tbGioHang.Rows.Add(intProductID, strProductName, strCatelogy, intQuantity, Weight, Width, Lenght, Price);
                GioHang:
                Session["Shopping"] = tbGioHang;
                Response.Redirect("ShoppingCard.aspx?ProID="+intProductID.ToString()+"");
            }
    protected void btnHome_Click(object sender, EventArgs e)
    {
        Response.Redirect("Home.aspx");
    }

  
}
